package com.controller;

import com.websocket.WebSocketServer;
import com.wrapper.ResponseWrapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author Fang Ruichuan
 * @date 2022-09-24 15:04
 */
@RestController
public class WebSocketController implements InitializingBean {

    @Value("${mySocket.myPwd}")
    private String myPwd;

    public static String MY_PWD;

    @Override
    public void afterPropertiesSet() {
        MY_PWD = myPwd;
    }
    /**
     * webSocket链接是否成功
     * @author Fang Ruichuan
     * @date 2022/9/24 15:18
     * @param webSocketId
     * @return ResponseWrapper<Boolean>
     */
    @GetMapping("/webSocketIsConnect/{webSocketId}")
    public ResponseWrapper<Boolean> webSocketIsConnect(@PathVariable("webSocketId") String webSocketId) {
        return ResponseWrapper.success(WebSocketServer.isConnected(webSocketId));
    }

    /**
     * webSocket发送客户端消息
     * @author Fang Ruichuan
     * @date 2022/9/24 15:18
     * @param webSocketId
     * @param message
     * @return ResponseWrapper<Boolean>
     */
    @GetMapping("/sendMessageByWayBillId")
    public ResponseWrapper<Boolean> sendMessageByWayBillId(String webSocketId, String message, String pwd) {
        boolean flag = false;
        if (MY_PWD.equals(pwd)) {
            flag = WebSocketServer.sendMessageByWayBillId(webSocketId, message);
        }
        return ResponseWrapper.success(flag);
    }

    /**
     * 群发消息
     * @author Fang Ruichuan
     * @date 2022/9/24 16:18
     * @param message
     * @param pwd
     */
    @GetMapping("/broadSendInfo")
    public void sendInfo(String message, String pwd) {
        if (MY_PWD.equals(pwd)) {
            WebSocketServer.sendInfo(message);
        }
    }

}
